package com.chcit.mobile.helper;

import android.content.Context;
import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.fastjson.JSONObject;
import com.chcit.mobile.R;
import com.chcit.mobile.app.contains.HttpMethodContains;
import com.chcit.mobile.app.utils.ToastUtils;
import com.chcit.mobile.common.DataKeys;
import com.chcit.mobile.mvp.model.api.service.UserService;
import com.chcit.mobile.mvp.model.entity.ResultBean;
import com.chcit.mobile.mvp.model.entity.WareHose;
import com.elvishew.xlog.XLog;
import com.jess.arms.base.app.Quick;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.RepositoryManager;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.Subject;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import static com.jess.arms.base.app.Quick.getConfiguration;

public class DialogHelper {
    static ArrayList<WareHose> wares = Quick.getConfiguration(DataKeys.WAREHOUSE_LIST);
    public static void showWareSelect(Context context, MaterialDialog.ListCallbackSingleChoice callback) {
         int i = 0;

        if (Quick.getConfiguration(DataKeys.WAREHOUSE_ID) == null) {
            i = 0;
        } else {
            i = Quick.getConfiguration(DataKeys.WAREHOUSE_ID);
        }
        if(wares.size() == 1){
            setWareHouse(context,i,callback);
        }else{
            new MaterialDialog.Builder(context)
                    .title(R.string.woreHourse)
                    .items(wares)
                    //  .itemsDisabledIndices(1, 3)
                    .autoDismiss(false)
                    .itemsCallbackSingleChoice(i
                            , (dialog, itemView, which, text) -> {
                                dialog.dismiss();
                                Quick.withConfigure(DataKeys.WAREHOUSE_ID,which);
                                setWareHouse(context, which,callback);
                                return true;
                            }
                    )
                    .positiveText(R.string.md_choose_label)
                    .show();
        }

    }

    private static void setWareHouse(Context context, int which,MaterialDialog.ListCallbackSingleChoice callback) {
        JSONObject data = new JSONObject();
        data.put("warehouseId",wares.get(which).getId());
        AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
        appComponent.repositoryManager()
                .obtainRetrofitService(UserService.class)
                .request(HttpMethodContains.WAREHOUSE_SET_OK,data)
                .subscribeOn(Schedulers.io())
                .subscribe(new ErrorHandleSubscriber<JSONObject>(appComponent.rxErrorHandler()){

                    @Override
                    public void onNext(JSONObject result) {
                        ResultBean resultBean = result.toJavaObject(ResultBean.class);
                        if(resultBean.isSuccess()){
                            if(callback != null){
                                 callback.onSelection(null,null,which,null);
                            }
                        }else{
                            onError(new Exception(resultBean.getMsg()));
                        }

                    }

                });
    }

    public static void showWareSelect(Context context) {
        showWareSelect(context,null);
    }
}
